<?php
namespace app\admin\controller;
use app\common\controller\AdminController;
use app\common\model\Article as  articleModel;
use think\facade\View;


class Article extends AdminController {

    public function index(){
        if($this->request->isAjax()){
            $title = $this->request->param('title','');
            $where_title = [];
            if($title){
                $where_title = 'title like "%'.$title.'%" ';
            }
            $where_author = [];


            $model = new articleModel();
            $re['code'] = 0;
            $re['msg'] = '';
            $re['data'] = [];
            $re['count'] = 0;
            $res = $model
                ->order('id','desc')
                ->where($where_title)
                ->where($where_author)
                ->paginate(['list_rows'=>$this->limit,'page'=>$this->page])
                ->toArray();
            if(sizeof($res['data'])>0){
                $re['data'] = $res['data'];
                $re['count'] = $res['total'];
            }
            return $re;
        }else{

            return View::fetch();

        }
    }

    public function edit()
    {
        View::engine()->layout(false);
        $model = new articleModel();
        if($this->request->isPost())
        {
            $data = $this->request->param();
            if($this->request->param('id')){
                unset($data['id']);
                $data['updated_at'] = TIMESTAMP;
                $model->strict(false)->where('id',$this->request->param('id'))->save($data);
            }else{
                $model->strict(false)->insert($data);
            }
            $this->setActionLog('编辑文章',$this->admin_info['name'].'编辑文章：'.$this->request->param('id'));
            $status = [
                'status' => true,
                'msg' => '修改成功',
                'data' => []
            ];
            return  $status;
        }

        if($this->request->param('id'))
        {
            //编辑页面
            $info = $model->where(['id'=>$this->request->param('id')])->find();
            if(!$info)
            {
                return error_code(10000);
            }

            View::assign('data',$info);
        }
        return View::fetch();
    }


    public function add(){
        View::engine()->layout(false);
        $model = new articleModel();

        if($this->request->isPost())
        {
            $data = $this->request->param();
            $data['created_at'] = TIME;
            $data['updated_at'] = TIME;
            $id = $model->strict(false)->insertGetId($data);

            $this->setActionLog('添加文章',$this->admin_info['name'].'添加文章：'.$id);

            $status = [
                'status' => true,
                'msg' => '添加成功',
                'data' => []
            ];
            return  $status;
        }
        return View::fetch();
    }

    public function del(){
        $result = [
            'status' => false,
            'msg' => error_code(10037,true),
            'data' => ''
        ];
        $id = $this->request->param('id');
        $model = new articleModel();
        $res = $model->where('id',$id)->delete();
        if($res) {
            $this->setActionLog('删除文章',$this->admin_info['name'].'删除文章：'.$id);

            $result['status'] = true;
            $result['msg'] = '删除成功';
        }else{
            $result['msg'] = '删除失败，请联系管理员';
        }
        return $result;
    }

}
